package cn.bugDance.domain.credit.model.aggregate;

import cn.bugDance.domain.credit.event.CreditPaySuccessEvent;
import cn.bugDance.domain.credit.model.entity.TaskEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditAccountEntity;
import cn.bugDance.domain.credit.model.entity.UserCreditOrderEntity;
import cn.bugDance.domain.credit.model.valobj.CreditPayVO;
import cn.bugDance.domain.credit.model.valobj.TaskStateVO;
import cn.bugDance.domain.credit.model.valobj.TradeNameVO;
import cn.bugDance.domain.credit.model.valobj.TradeTypeVO;
import cn.bugDance.types.response.BaseEvent;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Date;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreditAdjustAggregate {
    private String userId;
    private UserCreditOrderEntity userCreditOrderEntity;
    private UserCreditAccountEntity userCreditAccountEntity;
    private TaskEntity taskEntity;

    public static UserCreditAccountEntity createUserCreditAccountEntity(String userId, Integer adjustAmount, TradeTypeVO tradeType) {
        if(TradeTypeVO.FORWARD.getCode().equals(tradeType.getCode())){
            return UserCreditAccountEntity.builder()
                    .userId(userId)
                    .totalAmount(adjustAmount * tradeType.getOpt())
                    .availableAmount(adjustAmount * tradeType.getOpt()).build();
        }
        return UserCreditAccountEntity.builder()
                .userId(userId)
                .totalAmount(0)
                .availableAmount(adjustAmount * tradeType.getOpt()).build();
    }

    public static UserCreditOrderEntity createUserCreditOrderEntity(String userId, TradeNameVO tradeName, TradeTypeVO tradeType, Integer adjustAmount, String bizId) {
        return UserCreditOrderEntity.builder()
                .userId(userId)
                .bizId(bizId)
                .orderId(RandomStringUtils.randomNumeric(12))
                .tradeTime(new Date())
                .tradeType(tradeType)
                .tradeName(tradeName)
                .tradeAmount(adjustAmount).build();
    }


    public static TaskEntity createTaskEntity(String userId, CreditPayVO creditPay) {
        return TaskEntity.builder()
                .userId(userId)
                .message(CreditPaySuccessEvent.create(creditPay))
                .state(TaskStateVO.NOT_COMPLETED)
                .topic(CreditPaySuccessEvent.topic).build();
    }
}
